package com.fjb.controller.user;


import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonResult;
import com.fjb.common.enums.user.AddressStatus;
import com.fjb.controller.base.BaseController;
import com.fjb.dto.LoginUserInfo;
import com.fjb.pojo.user.SysUserAddress;
import com.fjb.service.user.SysUserAddressService;

/**
 * <p>
 * 用户地址 前端控制器
 * </p>
 *
 * @author hemiao
 * @since 2020-02-08
 */
@Controller
@RequestMapping("/userAddress")
public class SysUserAddressController extends BaseController{
	
	@Autowired
	private SysUserAddressService sysUserAddressService;
	
	/**
	 * @Description:查询收货地址列表
	 * @param request
	 * @return
	 * JsonResult<List<SysUserAddress>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月8日 下午9:00:32
	 */
	@RequestMapping(value="/selectList")
	@ResponseBody
	public JsonResult<List<SysUserAddress>> selectList(HttpServletRequest request){
		LoginUserInfo userInfo = getBaseWxTokenId(request);
		Integer userId = userInfo.getUserId();
		QueryWrapper<SysUserAddress> queryWrapper = new QueryWrapper<SysUserAddress>();
		queryWrapper.eq("user_id", userId);
		queryWrapper.eq("address_status", AddressStatus.STATUS_1.getCode());
		List<SysUserAddress> infoList = sysUserAddressService.list(queryWrapper);
		return new JsonResult<List<SysUserAddress>>(infoList, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:添加用户地址
	 * @param request
	 * @param isDefault
	 * @return
	 * JsonResult<Boolean>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月8日 下午11:09:22
	 */
	@RequestMapping(value="/addInfo")
	@ResponseBody
	public JsonResult<Boolean> addInfo(HttpServletRequest request,Boolean isDefault){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(isDefault==null) {
			return new JsonResult<Boolean>(null, httpCode);
		}
		String consigneeName = request.getParameter("consigneeName");
		if(StringUtils.isBlank(consigneeName)) {
			httpCode.setMsg("收货人名称不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		
		String consigneePhone = request.getParameter("consigneePhone");
		if(StringUtils.isBlank(consigneePhone)) {
			httpCode.setMsg("收货人电话不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		
		String province = request.getParameter("province");
		if(StringUtils.isBlank(province)) {
			httpCode.setMsg("省份不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String city = request.getParameter("city");
		if(StringUtils.isBlank(city)) {
			httpCode.setMsg("市不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String county = request.getParameter("county");
		
		String address = request.getParameter("address");
		
		LoginUserInfo userInfo = getBaseWxTokenId(request);
		Integer userId = userInfo.getUserId();
		Integer mainUserId = userInfo.getMainUserId();
		
		SysUserAddress ua = new SysUserAddress();
		ua.setMainUserId(mainUserId);
		ua.setUserId(userId);
		ua.setConsigneeName(consigneeName);
		ua.setConsigneePhone(consigneePhone);
		ua.setProvince(province);
		ua.setCity(city);
		if(StringUtils.isNotBlank(county)) {
			ua.setCounty(county);
		}
		if(StringUtils.isNotBlank(address)) {
			ua.setAddress(address);
		}
		if(isDefault.equals(true)) {
			// 更新所有的收货地址 不是默认
			QueryWrapper<SysUserAddress> updateWrapper = new QueryWrapper<SysUserAddress>();
			updateWrapper.eq("user_id", userId);
			SysUserAddress updateAddress = new SysUserAddress();
			updateAddress.setDefaultStatus(2);
			sysUserAddressService.update(updateAddress, updateWrapper);
			// 新加的未默认
			ua.setDefaultStatus(1);
		}else {
			ua.setDefaultStatus(2);
		}	
		ua.setCreateTime(LocalDateTime.now());
		ua.setCreateUserId(userId);	
		ua.setAddressStatus(AddressStatus.STATUS_1.getCode());
		boolean b = sysUserAddressService.save(ua);
		if(b==false) {
			httpCode.setMsg("保存失败,请稍后再试");
			return new JsonResult<Boolean>(b, httpCode);
		}
		return new JsonResult<Boolean>(null, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:删除
	 * @param request
	 * @param addressId
	 * @return
	 * JsonResult<Boolean>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月8日 下午11:17:16
	 */
	@RequestMapping(value="/deleteById")
	@ResponseBody
	public JsonResult<Boolean> deleteById(HttpServletRequest request,Integer addressId){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(addressId==null) {
			return new JsonResult<Boolean>(null, httpCode);
		}
		LoginUserInfo userInfo = getBaseWxTokenId(request);
		Integer userId = userInfo.getUserId();
		QueryWrapper<SysUserAddress> queryWrapper = new QueryWrapper<SysUserAddress>();
		queryWrapper.eq("id", addressId);
		queryWrapper.eq("user_id", userId);
		boolean b = sysUserAddressService.remove(queryWrapper);
		if(b==false) {	
			httpCode.setMsg("删除失败,请稍后再试");
			return new JsonResult<Boolean>(b, httpCode);
		}
		return new JsonResult<Boolean>(null, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:TODO
	 * @param request
	 * @param isDefault
	 * @param addressId
	 * @return
	 * JsonResult<Boolean>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月8日 下午11:17:26
	 */
	@RequestMapping(value="/updateById")
	@ResponseBody
	public JsonResult<Boolean> updateById(HttpServletRequest request,Boolean isDefault,Integer addressId){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(addressId==null) {
			return new JsonResult<Boolean>(null, httpCode);
		}
		if(isDefault==null) {
			httpCode.setMsg("产品id不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String consigneeName = request.getParameter("consigneeName");
		if(StringUtils.isBlank(consigneeName)) {
			httpCode.setMsg("收货人名称不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		
		String consigneePhone = request.getParameter("consigneePhone");
		if(StringUtils.isBlank(consigneePhone)) {
			httpCode.setMsg("收货人电话不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		
		String province = request.getParameter("province");
		if(StringUtils.isBlank(province)) {
			httpCode.setMsg("省份不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String city = request.getParameter("city");
		if(StringUtils.isBlank(city)) {
			httpCode.setMsg("市不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String county = request.getParameter("county");
		
		String address = request.getParameter("address");
		
		LoginUserInfo userInfo = getBaseWxTokenId(request);
		Integer userId = userInfo.getUserId();
		Integer mainUserId = userInfo.getMainUserId();
		
		SysUserAddress ua = new SysUserAddress();
		ua.setId(addressId);
		ua.setMainUserId(mainUserId);
		ua.setUserId(userId);
		ua.setConsigneeName(consigneeName);
		ua.setConsigneePhone(consigneePhone);
		ua.setProvince(province);
		ua.setCity(city);
		ua.setUpdateTime(LocalDateTime.now());
		ua.setUpdateUserId(userId);
		if(StringUtils.isNotBlank(county)) {
			ua.setCounty(county);
		}
		if(StringUtils.isNotBlank(address)) {
			ua.setAddress(address);
		}
		if(isDefault.equals(true)) {
			// 更新所有的收货地址 不是默认
			QueryWrapper<SysUserAddress> updateWrapper = new QueryWrapper<SysUserAddress>();
			updateWrapper.eq("user_id", userId);
			SysUserAddress updateAddress = new SysUserAddress();
			updateAddress.setDefaultStatus(2);
			sysUserAddressService.update(updateAddress, updateWrapper);
			// 新加的未默认
			ua.setDefaultStatus(1);
		}else {
			ua.setDefaultStatus(2);
		}
		boolean b = sysUserAddressService.updateById(ua);
		if(b==false) {
			httpCode.setMsg("修改失败,请稍后再试");
			return new JsonResult<Boolean>(b, httpCode);
		}
		return new JsonResult<Boolean>(null, HttpCode.SUCCESS);
	}
}
